﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using starLiGHT.Collections;

#endregion

#region COPYRIGHT

/*
    Copyright (c) 2008, 2009, 2010 
        Roland Rosenkranz (Glatzemann@email.de)
*/

#endregion

#region LICENSE

/*
    This file is part of starLiGHT.Collision.

    starLiGHT.Collision is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    starLiGHT.Collision is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with starLiGHT.Collision.  If not, see <http://www.gnu.org/licenses/>.

    
    ADDITIONAL (commercial) LICENSES for starLiGHT.Collision are available on request.
*/

#endregion

#region Box2D Copyright
/*
* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#endregion

#region Version Stuff
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 1486                  $:  Revision of last commit *
// * $Author:: glatzemann         $:  Author of last commit   *
// * $Date:: 2010-10-08 15:55:47 #$:  Date of last commit     *
// ************************************************************

// **************[ Box2D-Repository Info ]*********************
// Header-File: http://code.google.com/p/box2d/source/browse/trunk/Box2D/Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.h
// Revision   : r109
// Change-Date: 2010-05-31
//
// Source-File: http://code.google.com/p/box2d/source/browse/trunk/Box2D/Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.cpp
// Revision   : r109
// Change-Date: 2010-05-31
//
// Status     : Finished!
#endregion

namespace starLiGHT.Physic.TwoD
{
    public class PolyAndCircleContactPool : StaticPool<PolyAndCircleContact>
    {
        public PolyAndCircleContactPool()
            : base(4096)
        {

        }

        protected override PolyAndCircleContact AllocateNew()
        {
            return new PolyAndCircleContact();
        }
    }

    public class PolyAndCircleContact : Contact
    {
        static PolyAndCircleContactPool contactPool = new PolyAndCircleContactPool();

        public PolyAndCircleContact()
        {
        }

        new public static Contact Create(Fixture fixtureA, int indexA, Fixture fixtureB, int indexB)
        {
            //return new PolyAndCircleContact(fixtureA, fixtureB);

            PolyAndCircleContact cc = contactPool.Allocate();
            cc.Initialize(fixtureA, indexA, fixtureB, indexB);
            return cc;
        }

	    public new static void Destroy(ref Contact contact)
        {
            //contact = null;

            contactPool.Release((PolyAndCircleContact)contact);
        }

	    public PolyAndCircleContact(Fixture fixtureA, Fixture fixtureB)
            : base(fixtureA, 0, fixtureB, 0)
        {
            System.Diagnostics.Debug.Assert(m_fixtureA.GetType() == ShapeType.POLYGON);
            System.Diagnostics.Debug.Assert(m_fixtureB.GetType() == ShapeType.CIRCLE);
        }

	    public override void Evaluate(out Manifold manifold, ref Transform xfA, ref Transform xfB)
        {
            Collision.CollidePolygonAndCircle(out manifold,
                                              (PolygonShape)m_fixtureA.GetShape(), ref xfA,
                                              (CircleShape)m_fixtureB.GetShape(), ref xfB);
        }
    }
}
